//
//  MovieAllDetail.m
//  MINFOBBS
//
//  Created by juvid on 13-12-2.
//  Copyright (c) 2013年 minfo. All rights reserved.
//

#import "MovieAllDetail.h"
#import "DetectionSystemVersion.h"
#import "UIColor+expanded.h"
#import "MovieRole.h"
#import "MovieSong.h"
#import "SBJson.h"
@interface MovieAllDetail (){
    UIView *vieAll;
    MovieRole *movieRole;
    MovieSong *movieSong;
    int theCount;

}

@end

@implementation MovieAllDetail

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([[DetectionSystemVersion alloc]Version]) {
        self.edgesForExtendedLayout=UIRectEdgeNone;
    }
    self.vieOut.frame=CGRectMake(0, 30, 320, 300);
    [self.view addSubview:self.vieOut];
    [self BackButton];
    [self BtnOption];
    [self GetDatas];
    // Do any additional setup after loading the view from its nib.
}
-(void)BackButton{
    self.navigationItem.titleView=[DetectionSystemVersion SetBarTitle:@"百事搜"];
    [self.navigationItem setHidesBackButton:YES];
    UIButton *btnBack=[[DetectionSystemVersion alloc]BackButton];
    [btnBack addTarget:self action:@selector(BackReturn:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:btnBack];
    self.navigationItem.leftBarButtonItem=backItem;
    
}
#pragma mark 导航栏按钮事件
-(void)BackReturn:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillDisappear:(BOOL)animated{
    [tRequest clearDelegatesAndCancel];
    [tRequest cancel];
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [NSThread cancelPreviousPerformRequestsWithTarget:self];
}
#pragma mark 异步数据请求
-(void)GetDatas{
    tRequest=[[[ScreenAccess alloc]init]ASIGetSearch:self.strKey Start:0 Limit:5];
    [tRequest setDelegate:self];
    [tRequest setUsername:@"allMessage"];
    
}
-(void)requestFinished:(ASIHTTPRequest*)request{
    
    NSString *response = [request responseString];
    SBJsonParser * parser = [[SBJsonParser alloc] init];
    NSMutableDictionary *Result = [parser objectWithString:response];
    
    if ([request.username isEqual:@"allMessage"]){
        BOOL IsSuccess=[[[Result objectForKey:@"Response"]objectForKey:@"status"]integerValue]==200?YES:NO;
        if (IsSuccess) {
            
            self.arrReturn=[[[[[[Result objectForKey:@"Response"] objectForKey:@"Categories"] objectForKey:@"Category"] objectAtIndex:0]objectForKey:@"Links"] objectForKey:@"Link"];
            [self ShowData:[[[[[[[Result objectForKey:@"Response"] objectForKey:@"Categories"] objectForKey:@"Category"] objectAtIndex:0]objectForKey:@"Items"] objectForKey:@"Item"] objectAtIndex:0]];
        }
    }
    
    
}
-(void)ShowData:(NSDictionary *)dic{
    int bt1=0,bt2=0;
    for (int i=0; i<self.arrReturn.count; i++) {
        if ([[[self.arrReturn  objectAtIndex:i] objectForKey:@"text"]isEqual:@"上一集"]) {
            bt1=1;
        }
        else if([[[self.arrReturn  objectAtIndex:i] objectForKey:@"text"]isEqual:@"下一集"]){
            bt2=1;
        }
    }
    if (bt1==1) {
        self.btnUp.hidden=NO;
    }
    else{
        self.btnUp.hidden=YES;
    }
    if (bt2==1) {
        self.btnNext.hidden=NO;
    }
    else{
        self.btnNext.hidden=YES;
    }
    NSString *str=[[[[dic objectForKey:@"Properties"] objectForKey:@"Property"] objectAtIndex:0] objectForKey:@"text"];
    CGSize labSize=[DetectionSystemVersion String:str Width:285 Font:15];
    self.labContent.frame=CGRectMake(self.labContent.frame.origin.x, self.labContent.frame.origin.y, 285, labSize.height);
    self.vieBtn.frame=CGRectMake(0, self.labContent.frame.origin.y+self.labContent.frame.size.height+20, 320  , 30);
    if ( self.vieBtn.frame.origin.y+80>self.view.frame.size.height) {
            self.vieOut.frame=CGRectMake(0, 30, 320,self.view.frame.size.height-30);
    }
    else{
            self.vieOut.frame=CGRectMake(0, 30, 320, self.vieBtn.frame.origin.y+50);
    }
    self.scrVie.contentSize=CGSizeMake(320, self.vieBtn.frame.origin.y+50);
    self.labContent.text=str;
    self.labTitle.text=[dic objectForKey:@"Title"];
    
}
-(void)BtnOption{
    NSArray *arr;
    if (self.isSong) {
        arr=[[NSArray alloc]initWithObjects:@"剧情",@"角色介绍", nil];

    }
    else{
    arr=[[NSArray alloc]initWithObjects:@"剧情",@"角色介绍",@"影视歌曲", nil];
    }
   vieAll=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    vieAll.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:vieAll];
    for (int i=0; i<arr.count; i++) {
        UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0+(320/arr.count)*i, 0, 320/arr.count, 30)];
        [btn setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(PressNextCons:) forControlEvents:UIControlEventTouchUpInside];
        if (i==0) {
            [btn setBackgroundImage:[UIImage imageNamed:@"constellOption"] forState:UIControlStateNormal];
            
        }
        else{
            [btn setBackgroundImage:[UIImage imageNamed:@"constellOptionUn"] forState:UIControlStateNormal];
            
        }
        [vieAll addSubview:btn];
    }
}
-(void)PressNextCons:(UIButton *)button{
    for (UIView *subvie in vieAll.subviews) {
        if ([subvie isKindOfClass:[UIButton class]]) {
            UIButton *btn=(UIButton *)subvie;
            [btn setBackgroundImage:[UIImage imageNamed:@"constellOptionUn"] forState:UIControlStateNormal];
        }
    }
    if ([button.currentTitle isEqual:@"剧情"]) {
       
        if (theCount!=0) {
            [self.view addSubview:self.vieOut];
            [movieSong.view removeFromSuperview];
            [movieSong removeFromParentViewController];
            [movieRole.view removeFromSuperview];
            [movieRole removeFromParentViewController];
             theCount=0;
        }
        
    }
    else if([button.currentTitle isEqual:@"角色介绍"]){
        if (theCount!=1) {
            if (movieRole==Nil) {
                movieRole=[[MovieRole alloc]init];
                movieRole.strKey= [self  GetQuery:@"角色介绍"];
                movieRole.isAdd=YES;
                movieRole.view.frame=CGRectMake(0, 30, 320, self.view.frame.size.height-30);
            }
            [self.vieOut removeFromSuperview];
            [movieSong.view removeFromSuperview];
            [movieSong removeFromParentViewController];
            [self.view addSubview:movieRole.view];
            [self addChildViewController:movieRole];
            theCount=1;
        }
        
        
        
    }
    else if([button.currentTitle isEqual:@"影视歌曲"]){
        if (theCount!=2) {
            if (movieSong==Nil) {
                movieSong=[[MovieSong alloc]init];
                movieSong.strKey= [self  GetQuery:@"影视歌词"];
                movieSong.isAdd=YES;
                movieSong.view.frame=CGRectMake(0, 30, 320, self.view.frame.size.height-30);
            }
            [self.vieOut removeFromSuperview];
            [movieRole.view removeFromSuperview];
            [movieRole removeFromParentViewController];
            
            [self.view addSubview:movieSong.view];
            [self addChildViewController:movieSong];
            theCount=2;
        }
      
    }
//    if ([[self.arrReturn objectAtIndex:theCount]isKindOfClass:[NSString class]]) {
//        [self GetDatas:[arrUrl objectAtIndex:theCount-1]];
//    }
//    else{
//        [self SetCellHight:[[[self.arrReturn objectAtIndex:theCount] objectForKey:@"Properties"] objectForKey:@"Property"]];
//    }
    [button setBackgroundImage:[UIImage imageNamed:@"constellOption"] forState:UIControlStateNormal];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)PrseeUp:(id)sender {
   [self NextData:0];
}

- (IBAction)PressNext:(id)sender {
    if (self.btnUp.hidden) {
        [self NextData:0];
    }
    else{
    [self NextData:1];
    }
}
-(void)NextData:(int)count{
    NSString *str=[[self.arrReturn  objectAtIndex:count] objectForKey:@"href"];
    //
    NSRange range = [str rangeOfString:@"query="];
    self.strKey=[str substringFromIndex:range.location +range.length];
    [self GetDatas];
}
//-(NSString *)GetQuery:(int)count{
//    NSString *str=[[self.arrReturn objectAtIndex:count] objectForKey:@"href"];
//    //
//    NSRange range = [str rangeOfString:@"query="];
//    str=[str substringFromIndex:range.location +range.length];
//    return str;
//    
//}
-(NSString *)GetQuery:(NSString *)query{
    NSString *str;
    for (int i=0; i<self.arrReturn.count; i++) {
        if ([[[self.arrReturn objectAtIndex:i] objectForKey:@"text"] isEqual:query]) {
            str=[[self.arrReturn objectAtIndex:i] objectForKey:@"href"];
            NSRange range = [str rangeOfString:@"query="];
            str=[str substringFromIndex:range.location +range.length];
        }
    }
    //
    return str;
    
}
@end
